<template>
  <el-dialog
    :visible="value"
    title="辅助检查"
    width="700px"
    @close="onClose"
    @open="onOpen"
    :append-to-body="true"
    :close-on-click-modal="false"
  >
    <el-form :model="form" :rules="rules" ref="form" label-width="120px">
      <el-form-item label="既往辅助检查：">
        <el-select
          v-model="assistId"
          value-key="id"
          @change="changeAssist"
          placeholder="请选择需要修改的辅助检查"
        >
          <el-option label="----------" key="-1" value></el-option>
          <el-option v-for="(item,index) in fileList" :key="index" :label="item.name" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称：">
        <el-input v-model="form.name" maxlength="50" show-word-limit></el-input>
      </el-form-item> 
      <el-form-item label="上传附件：">
        <moreImg v-model="file" :limitType="fileObj.limitType"></moreImg>
        <div>
          <div v-for="(item,index) in file" :key="index">
            <el-link type="primary" @click="onClickFile(item)">{{item}}</el-link>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="描述：">
        <el-input type="textarea" v-model="form.remark" maxlength="200" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="结论：">
        <el-input type="textarea" v-model="form.result" maxlength="200" show-word-limit></el-input>
      </el-form-item>
      <el-button v-show="false" :loading="btnLoading" native-type="submit">提交</el-button>
    </el-form>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "curItem"],
  data() {
    return {
      form: {
        id: "",
        hpId: "",
        cureId: "",
        name: "",
        type: "1", //
        imgUrls: "",
        reportTime: "",
        result: "",
        remark: ""
      },
      assistId: "",
      file: [],
      rules: {},
      fileObj: {
        limitType: ["image/png", "image/jpeg", "application/pdf"],
        fileSize: 10240
      },
      fileList: [],
      btnLoading: false
    };
  },
  methods: {
    onOpen() {
      const th = this;
      th.$nextTick(function() {
        if (th.curItem) {
          th.form.cureId = th.curItem.cureMedicalInfoResponse.cureId;
          th.form.name = th.curItem.cureAssistInfo;
          th.form.reportTime = th.curItem.treatmentDate;
          th.$api.getPatientReportById({ cureId: th.form.cureId }).then(res => {
            if (res.executed) {
              let list = res.data || [];
              th.fileList = list;
              if (list.length > 0) {
                let obj = list[0];
                th.file = obj.imgUrls;
                th.form = Object.assign(th.form, obj);
                th.assistId = obj;
              }
            }
          });
        } else {
          th.$refs["form"].resetFields();
        }
      });
    },
    //点击图片
    onClickFile(file) {
      const th = this;
      if (typeof file == "object") {
        if (file.type == "image/jpeg" || file.type == "image/png") {
          th.$utils.fileToDataUrl(file, function(url) {
            window.open(url);
          });
        }
      } else {
        let url = th.$config.imageUrl + file;
        window.open(url);
      }
    },
    onSubmit() {
      let th = this;
      let j = {};
      Object.assign(j, th.form);
      j.imgUrls = JSON.stringify(th.file);
      th.btnLoading = true;
      let add = () => {
        th.$api
          .addPatientReport(j)
          .then(res => {
            if (res.executed) {
              th.$message.success(res.message);
              th.btnLoading = false;
              th.onClose();
            } else {
              th.$message.error(res.message);
              th.btnLoading = false;
            }
          })
          .catch(error => {
            th.$message.error("服务器异常");
            th.btnLoading = false;
          });
      };
      let edit = () => {
        th.$api
          .editPatientReport(j)
          .then(res => {
            if (res.executed) {
              th.$message.success(res.message);
              th.btnLoading = false;
              th.onClose();
            } else {
              th.$message.error(res.message);
              th.btnLoading = false;
            }
          })
          .catch(error => {
            th.$message.error("服务器异常");
            th.btnLoading = false;
          });
      };
      if (th.form.id == "") {
        add();
      } else {
        edit();
      }
    },
    onClose() {
      let th = this;
      th.$emit("input", false);
      th.$refs["form"].resetFields();
    },
    changeAssist(obj) {
      let th = this;
      if (obj == "") {
        th.form.id='';
        th.form.name='';
        th.form.imgUrls=""
        th.form.reportTime= ""
        th.form.result= ""
        th.form.remark= ""
        th.file=[]
      } else {
        th.file = obj.imgUrls;
        th.form = Object.assign(th.form, obj);
      }
    }
  },
  components: {
    moreImg: () => import("@/components/largeUpFile")
  }
};
</script>